package com.cfk.shopmail.product.web;

/*
    @CreateTime 2021/12/14 14:34
    @CreateBy cfk
*/

import com.cfk.shopmail.product.entity.CategoryEntity;
import com.cfk.shopmail.product.service.CategoryService;
import com.cfk.shopmail.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {

        //TODO 1.查看所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevelOneCategorys();

        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String getHello() {
        RLock lock = redisson.getLock("my-lock");
        lock.lock();

        try {
            System.out.println(Thread.currentThread().getId()+"获得了锁");
            Thread.sleep(30000);
        }catch (Exception e) {

        }finally {
            System.out.println(Thread.currentThread().getId()+"释放了锁");
            lock.unlock();
        }

        return "Hello";
    }
}
